/**
 * 全站http配置
 *
 * axios参数说明
 * isSerialize是否开启form表单提交
 * isToken是否需要token
 */
import axios from "axios";

//默认超时时间
axios.defaults.timeout = 100000;
//返回其他状态码
axios.defaults.validateStatus = function (status) {
  return status >= 200 && status <= 500;
};
//跨域请求，允许保存cookie
axios.defaults.withCredentials = true;
//http request拦截
axios.interceptors.request.use(
  (config) => {



    //headers中配置text请求
    if (config.text === true) {
      config.headers["Content-Type"] = "text/plain";
    }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);
//http response 拦截
let timer = null;
axios.interceptors.response.use(
  (res) => {

    //获取状态码
    const status = res.data.code || res.status;
    const message = res.data.msg || res.data.error_description || "未知错误";
    //如果是401则跳转到登录页面
    // 如果请求为500否者默认统一处理
    if (status === 500) {
      if (!timer) {
        timer = true;
       console.log('请求失败，请联系管理员')
      }
      // return Promise.reject(new Error(message))
      return Promise.reject(message);
    }

    return res.data;
  },
  (error) => {

    return Promise.reject(new Error(error));
  }
);

export default axios;
